/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.uncopt.ohos.example.justify;

import com.uncopt.ohos.widget.text.justify.JustifiedEditText;
import com.uncopt.ohos.widget.text.justify.JustifiedTextView;
import com.uncopt.ohos.widget.text.justify.RichTextBean;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.text.RichText;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class MainProvider extends PageSliderProvider implements Component.ClickedListener,
        JustifiedTextView.RichTextTouchListener {
    private static final String INTENT_ACTION = "android.intent.action.VIEW";

    private final Context mContext;
    private JustifiedTextView mJustifiedText;

    public MainProvider(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        Component view;
        if (i == 0) {
            view = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_text, null, false);
            mJustifiedText = (JustifiedTextView) view.findComponentById(ResourceTable.Id_text_1);
            mJustifiedText.setRichTextData(getTextDataList());
            mJustifiedText.addDrawTask(mJustifiedText);
            mJustifiedText.setRichTextTouchListener(this);
            mJustifiedText.setClickedListener(MainProvider.this);
        } else {
            view = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_edit, null, false);
            JustifiedEditText justifiedParagraph =
                    (JustifiedEditText) view.findComponentById(ResourceTable.Id_edit_justified);
            justifiedParagraph.addDrawTask(justifiedParagraph);
            justifiedParagraph.setText(ResourceTable.String_lorem_ipsum_extended);
        }
        componentContainer.addComponent(view);
        return view;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }

    private List<RichTextBean> getTextDataList() {
        List<RichTextBean> richTextBeanList = new ArrayList<>();
//        if (index == 1) {
        richTextBeanList.add(new RichTextBean("The library used in this demo is available on ",
                mJustifiedText.getTextNormal()));
        richTextBeanList.add(new RichTextBean("Github", mJustifiedText.getTextFormURL()));
        richTextBeanList.add(new RichTextBean(". It  \u0009was \u0009 written \u0009 by", mJustifiedText.getTextNormal()));
        richTextBeanList.add(new RichTextBean(" Jerome DAVID", mJustifiedText.getTextBold()));
        richTextBeanList.add(new RichTextBean(", an Ohos developer from UNCOPT LLC.\n",
                mJustifiedText.getTextNormal()));
//            return richTextBeanList;
//        } else if (index == 2) {
        richTextBeanList.add(new RichTextBean("UNCOPT  \u0009LLC is a small company based in Houston, " +
                "TX and was funded in 2011 by two developers: Jerome DAVID and Christophe CASADEBAIG.  \u0009 \u0009" +
                "It  \u0009 \u0009specializes  \u0009 \u0009in \u0009 \u0009 Ohos development.\n", mJustifiedText.getTextNormal()));
//            return richTextBeanList;
//        } else if (index == 3) {
        richTextBeanList.add(new RichTextBean("The first app published by UNCOPT was ",
                mJustifiedText.getTextNormal()));
        richTextBeanList.add(new RichTextBean("Duo: Holo File Manager w/ Root (DuoFM)",
                mJustifiedText.getTextFormURL()));
        richTextBeanList.add(new RichTextBean(". It'sa file manager that focuses on security, but also on performance and ease of use. You can read more about it ", mJustifiedText.getTextNormal()));
        richTextBeanList.add(new RichTextBean("here", mJustifiedText.getTextFormURL()));
        richTextBeanList.add(new RichTextBean(" Plugins for this file manager are available (for free) and " +
                "are distributed as separate applications.\n", mJustifiedText.getTextNormal()));
//            return richTextBeanList;
//        } else if (index == 4) {
        richTextBeanList.add(new RichTextBean("The  \u0009second \u0009 app  \u0009published \u0009 was  \u0009a  \u0009small  \u0009developer  \u0009tools  \u0009called ",
                mJustifiedText.getTextNormal()));
        richTextBeanList.add(new RichTextBean("Character Codes", mJustifiedText.getTextFormURL()));
        richTextBeanList.add(new RichTextBean(" It's an app that conveniently displays character codes and " +
                "entity name.\n", mJustifiedText.getTextNormal()));
//            return richTextBeanList;
//        } else {
        richTextBeanList.add(new RichTextBean("<<DuoFM>> is available for free (with ads) and as  a  paid " +
                " version, while <<Character Codes>> is only available as \u0009 a \u0009 paid \u0009 app.  \u0009Since  \u0009the release of those two apps, ", mJustifiedText.getTextNormal()));
        richTextBeanList.add(new RichTextBean("new UNCOPT apps", mJustifiedText.getTextFormURL()));
        richTextBeanList.add(new RichTextBean(" have been released completely free with no ads \u0009 or \u0009 tracking  \u0009of  \u0009any kind. Instead, there's a ", mJustifiedText.getTextNormal()));
        richTextBeanList.add(new RichTextBean("Donation  \u0009app", mJustifiedText.getTextFormURL()));
        richTextBeanList.add(new RichTextBean("  \u0009and  \u0009users  \u0009can  \u0009show  \u0009their appreciation and help out by donating a little bit of money.",
                mJustifiedText.getTextNormal()));
        return richTextBeanList;
//        }
    }

    @Override
    public void onClick(Component component) {
        if (mJustifiedText.goNext) {
            DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_toast_layout, null, false);
            ((Text) toastLayout.findComponentById(ResourceTable.Id_msg_toast)).setText("Click");
            new ToastDialog(mContext)
                    .setContentCustomComponent(toastLayout)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setTransparent(true)
                    .show();
        } else {
            mJustifiedText.goNext = true;
        }
    }

    @Override
    public void onTouch(RichText richText) {
        richText.addTouchEventListener((component16, touchEvent) -> {
            // Github
            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
//                        Intent intent = new Intent();
//                        intent.setAction(INTENT_ACTION);
//                        Uri uri = Uri.parse("https://www.baidu.com");
//                        intent.setUri(uri);
//                        mContext.startAbility(intent, 0);
                mJustifiedText.goNext = false;
            }
            return true;
        }, mJustifiedText.getLength(0), mJustifiedText.getLength(1));
        richText.addTouchEventListener((component15, touchEvent) -> {
            // Duo: Holo File Manager w/ Root (DuoFM)
            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
                intent.setAction(INTENT_ACTION);
                Uri uri = Uri.parse("market://details?id=com.uncopt.android.filebrowser.free");
                intent.setUri(uri);
                mContext.startAbility(intent, 0);
                mJustifiedText.goNext = false;
            }
            return true;
        }, mJustifiedText.getLength(6) - 2, mJustifiedText.getLength(7) - 2);
        richText.addTouchEventListener((component14, touchEvent) -> {
            // here
            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
                Intent intent = new Intent();
                intent.setAction(INTENT_ACTION);
                Uri uri = Uri.parse(mContext.getString(ResourceTable.String_bitbucket));
                intent.setUri(uri);
                mContext.startAbility(intent, 0);
                mJustifiedText.goNext = false;
            }
            return true;
        }, mJustifiedText.getLength(8) - 2, mJustifiedText.getLength(9) - 2);
        richText.addTouchEventListener((component13, touchEvent) -> {
            // Character Codes
            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
                Intent intent = new Intent();
                intent.setAction(INTENT_ACTION);
                Uri uri = Uri.parse("market://details?id=com.uncopt.android.charcodes");
                intent.setUri(uri);
                intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
                mContext.startAbility(intent, 0);
                mJustifiedText.goNext = false;
            }
            return true;
        }, mJustifiedText.getLength(11) - 3, mJustifiedText.getLength(12) - 3);
        richText.addTouchEventListener((component12, touchEvent) -> {
            // new UNCOPT apps
            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
                Intent intent = new Intent();
                intent.setAction(INTENT_ACTION);
                Uri uri = Uri.parse(mContext.getString(ResourceTable.String_uncopt));
                intent.setUri(uri);
                mContext.startAbility(intent, 0);
                mJustifiedText.goNext = false;
            }
            return true;
        }, mJustifiedText.getLength(14) - 4, mJustifiedText.getLength(15) - 4);
        richText.addTouchEventListener((component1, touchEvent) -> {
            // Donation app
            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
                Intent intent = new Intent();
                intent.setAction(INTENT_ACTION);
                Uri uri = Uri.parse("market://details?id=com.uncopt.android.donations");
                intent.setUri(uri);
                intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
                mContext.startAbility(intent, 0);
                mJustifiedText.goNext = false;
            }
            return true;
        }, mJustifiedText.getLength(16) - 4, mJustifiedText.getLength(17) - 4);
    }
}
